package it.ma.musicplayernew.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.DataFormatException;

import android.util.Log;

/**
 * 命令集
 * 
 * @author victor
 * 
 */
public class CMDUtils {

	private static final String TAG = null;
	private static final boolean D = true;
	// 公共头
	public static String COMMON_HEAD = "AT#";
	//结束符
	public static String END_CHART = "*";
	// 上一曲
	public static String PREV = COMMON_HEAD + "01";
	// 下一曲
	public static String NEXT = COMMON_HEAD + "02";
	/**
	 * EQ 音效 0 NOR 正常 1 POP 流行 2 ROCK 摇滚 3 JAZZ 爵士 4 CLASS 经典 5 FLAT 平直
	 */
	public static String EQ = COMMON_HEAD + "03";
	// 音效-正常
	public static String EQ_NOR = EQ + "0";
	// 音效-流行
	public static String EQ_POP = EQ + "1";
	// 音效-摇滚
	public static String EQ_ROCK = EQ + "2";
	// 音效-爵士
	public static String EQ_JAZZ = EQ + "3";
	// 音效-经典
	public static String EQ_CLASS = EQ + "4";
	// 播放
	public static String PLAY = COMMON_HEAD + "041";
	// 暂停
	public static String PAUSE = COMMON_HEAD + "042";
	// 开机
	public static String POWERON = COMMON_HEAD + "051";
	// 开机状态下
	public static String ON_POWERON = COMMON_HEAD + "150";
	// 关机
	public static String POWEROFF = COMMON_HEAD + "052";
	// 开机状态下
	public static String ON_POWEROFF = COMMON_HEAD + "160";
	// 静音
	public static String MUTEON = COMMON_HEAD + "061";
	// 取消静音
	public static String MUTEOFF = COMMON_HEAD + "062";
	// 播放U盘
	public static String USB = COMMON_HEAD + "07";
	// 播放SD卡
	public static String SD = COMMON_HEAD + "08";
	// 外接音源
	public static String AUX = COMMON_HEAD + "09";
	// 收音机模式
	public static String RADIO = COMMON_HEAD + "10";
	// 蓝牙状态
	public static String BT = COMMON_HEAD + "11";
	// 灯光颜色
	public static String LIGHT = COMMON_HEAD + "12";

	// 亮灯色种选择AT#12255255255255
	public static String getLight(int R, int G, int B, int A) {
		return LIGHT + paddingZeroFromColor(R) + paddingZeroFromColor(G)
				+ paddingZeroFromColor(B) + paddingZeroFromColor(A);
	}

	// 随机播放
	public static String RDM = COMMON_HEAD + "13";
	// 单曲重复播放
	public static String RPT = COMMON_HEAD + "14";
	// 列表循环
	public static String ALL = COMMON_HEAD + "170";
	// 重低音调节-顺时针
	public static String BAS_CLOCKWISE = COMMON_HEAD + "151";
	// 重低音调节-逆时针
	public static String BAS_COUNTERCLOCKWISE = COMMON_HEAD + "152";
	// 高低音调节-顺时针
	public static String TRE_CLOCKWISE = COMMON_HEAD + "161";
	// 高低音调节-逆时针
	public static String TRE_COUNTERCLOCKWISE = COMMON_HEAD + "162";
	// 左右声道调节-顺时针
	public static String BAL_CLOCKWISE = COMMON_HEAD + "181";
	// 左右声道调节-逆时针
	public static String BAL_COUNTERCLOCKWISE = COMMON_HEAD + "182";
	// 前后声道调节-顺时针
	public static String FAD_CLOCKWISE = COMMON_HEAD + "192";
	// 前后声道调节-逆时针
	public static String FAD_COUNTERCLOCKWISE = COMMON_HEAD + "191";
	// 音量调节-顺时针
	public static String VOL_CLOCKWISE = COMMON_HEAD + "201";
	// 音量调节-逆时针
	public static String VOL_COUNTERCLOCKWISE = COMMON_HEAD + "202";
	// FM频率头， AT#21XXXXXX ，XXXX 如：8750~10800
	public static String FM_HEAD = COMMON_HEAD + "21";
	// 歌曲名头 ， N个字节
	public static String MUSIC_NAME_HEAD = COMMON_HEAD + "22";
	// 重低音调节接收头 AT#230/1/2+X
	public static String READ_BAS_HEAD = COMMON_HEAD + "23";
	//
	public static String READ_BAS_MIDDLE_HEAD = READ_BAS_HEAD + "0";
	//
	public static String READ_BAS_PLUS_HEAD = READ_BAS_HEAD + "1";
	//
	public static String READ_BAS_MINUS_HEAD = READ_BAS_HEAD + "2";
	// 高低音调节接收头 AT#240/1/2+X
	public static String READ_TRE_HEAD = COMMON_HEAD + "24";
	//
	public static String READ_TRE_MIDDLE_HEAD = READ_TRE_HEAD + "0";
	//
	public static String READ_TRE_PLUS_HEAD = READ_TRE_HEAD + "1";
	//
	public static String READ_TRE_MINUS_HEAD = READ_TRE_HEAD + "2";
	// 左右声道调节接收头 AT#250/1/2+X
	public static String READ_BAL_HEAD = COMMON_HEAD + "25";
	//
	public static String READ_BAL_MIDDLE_HEAD = READ_BAL_HEAD + "0";
	//
	public static String READ_BAL_PLUS_HEAD = READ_BAL_HEAD + "1";
	//
	public static String READ_BAL_MINUS_HEAD = READ_BAL_HEAD + "2";
	// 前后声道调节接收头 AT#260/1/2+X
	public static String READ_FAD_HEAD = COMMON_HEAD + "26";
	//
	public static String READ_FAD_MIDDLE_HEAD = READ_FAD_HEAD + "0";
	//
	public static String READ_FAD_PLUS_HEAD = READ_FAD_HEAD + "1";
	//
	public static String READ_FAD_MINUS_HEAD = READ_FAD_HEAD + "2";
	// 接收音量值 AT#27X
	public static String READ_VOL_HEAD = COMMON_HEAD + "27";
	// 音乐播放总时间 AT#28XXX XX XXX:分 XX:秒
	public static String MUSIC_TOTAL_TIME_HEAD = COMMON_HEAD + "28";
	// 当前播放音乐时间 AT#28XXX XX XXX:分 XX:秒
	public static String MUSIC_CURR_TIME_HEAD = COMMON_HEAD + "29";
	// 同步所有数据
	public static String SYNC = COMMON_HEAD + "30";
	// 时间同步
	public static String TIME_SYNC_HEAD = COMMON_HEAD + "31";

	// 99:33
	public static String readMusicTime(String readStr) {// readStr = AT#28XXXX
		if (!(readStr.startsWith(MUSIC_TOTAL_TIME_HEAD) || readStr
				.startsWith(MUSIC_CURR_TIME_HEAD))) {
			if (D)
				Log.e(TAG, "error MusicTime!!");
			return null;
		}
		try {
			StringBuffer result = new StringBuffer();
			String total_m = readStr.substring(5, 7);// 总分值
			result.append(total_m);
			result.append(":");
			result.append(readStr.substring(7, readStr.length())); // 秒
			return result.toString();
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, e.toString());
		}
		return "00:00";
	}
	
	// 1:15:33
	public static String readMusicTime2(String readStr) {// readStr = AT#28XXXX
		if (!(readStr.startsWith(MUSIC_TOTAL_TIME_HEAD) || readStr
				.startsWith(MUSIC_CURR_TIME_HEAD))) {
			if (D)
				Log.e(TAG, "error MusicTime!!");
			return null;
		}
		int h, m; // 时分秒
		StringBuffer result = new StringBuffer();
		Integer total_m = Integer.valueOf(readStr.substring(5, 7));// 总分值
		h = total_m / 60; // 小时值
		m = total_m % 60; // 小计分值
		if (h > 0) {
			if (h < 10) {
				result.append("0"); // 09 补零
			}
			result.append(h);
			result.append(":");
		}
		if (m > 0) {
			if (m < 10) {
				result.append("0"); // 09 补零
			}
			result.append(m);
			result.append(":");
		} else {
			result.append("00:"); // 00:
		}
		result.append(readStr.substring(7, readStr.length())); // 秒
		return result.toString();
	}
	/**
	 * 将字符串时间转换成整数总秒数
	 * @param timeStr  
	 * 				AT#28XXXX
	 * @return  整数时间 秒
	 */
	public static int parseTime(String timeStr){ //timeStr = AT#28XXXX
		try {
			String readMusicTime = readMusicTime(timeStr);
			String[] split = readMusicTime.split(":");
			int total = Integer.parseInt(split[0]) * 60 + Integer.parseInt(split[1]);
			return total;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		StringBuilder sb = new StringBuilder();
		return days + " days " + hours + " hours " + minutes + " minutes "
				+ seconds + " seconds ";
	}

	private static String paddingZeroFromColor(int i) {
		if (i > 255) // 最大值255
			return String.valueOf(255);
		else if (i <= 0)// 最小值0
			return "000";
		else {
			int t = i / 10;
			if (t <= 0) {
				return "00" + String.valueOf(i);
			} else if (t > 9) {
				return String.valueOf(i);
			} else {
				return "0" + String.valueOf(i);
			}
		}
	}

	/**
	 * 截取重低音调节值
	 * 
	 * @param readStr
	 * @return
	 */
	public static int getVolValue(String readStr) { // AT#27X
		if (!readStr.startsWith(READ_VOL_HEAD)) {
			if (D)
				Log.e(TAG, "no a VOL cmd");
			return -1;
		}
		int valueOf = 0;
		try {
			valueOf = Integer.valueOf(readStr.substring(5, readStr.length()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			if (D)
				Log.e(TAG, "截取音量值异常！！！");
		}
		return valueOf;
	}

	/**
	 * 截取重低音调节值
	 * 
	 * @param readStr
	 * @return
	 */
	public static String getBASValue(String readStr) {
		if (!readStr.startsWith(READ_BAS_HEAD)) {
			if (D)
				Log.e(TAG, "no a BAS cmd");
			return "0";
		}
		return getValue(readStr);
	}

	/**
	 * 截取高低音调节值
	 * 
	 * @param readStr
	 * @return
	 */
	public static String getTREValue(String readStr) {
		if (!readStr.startsWith(READ_TRE_HEAD)) {
			if (D)
				Log.e(TAG, "no a TRE cmd");
			return "0";
		}
		return getValue(readStr);
	}

	/**
	 * 截取左右声道调节值
	 * 
	 * @param readStr
	 * @return
	 */
	public static String getBALValue(String readStr) {
		if (!readStr.startsWith(READ_BAL_HEAD)) {
			if (D)
				Log.e(TAG, "no a BAL cmd");
			return "0";
		}
		String value = readStr.substring(6, readStr.length());

		String substring = readStr.substring(5, 6);
		if ("1".equals(substring)) { // 1 -- 正值
			return "L" + value;
		} else if ("2".equals(substring)) {// 2 -- 负值
			return "R" + value;
		} else { // 默认正 + 0
			return "0";
		}
	}

	/**
	 * 截取前后声道调节值
	 * 
	 * @param readStr
	 * @return
	 */
	public static String getFADValue(String readStr) {
		if (!readStr.startsWith(READ_FAD_HEAD)) {
			if (D)
				Log.e(TAG, "no a FAD cmd");
			return "0";
		}
		String value = readStr.substring(6, readStr.length());

		String substring = readStr.substring(5, 6);
		if ("1".equals(substring)) { // 1 -- 正值
			return "R" + value;
		} else if ("2".equals(substring)) {// 2 -- 负值
			return "F" + value;
		} else { // 默认正 + 0
			return "0";
		}
	}

	// x
	private static String getValue(String readStr) {// readStr AT#231X 或 readStr
		int valueOf = 0;
		try {
			valueOf = Integer.valueOf(readStr.substring(6, readStr.length()));
			if(valueOf == 0){
				return "0";
			}
			String substring = readStr.substring(5, 6);
			if ("1".equals(substring)) { // 1 -- 正值
				return "+" + valueOf;
			} else if ("2".equals(substring)) {// 2 -- 负值
				return "-" + valueOf;
			} else { // 默认正 + 0
				return "0";
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			if (D)
				Log.e(TAG, "格式化数字异常！！");
		}
		return "0";
	}

	/**
	 * 拼装FM频率指令
	 * 
	 * @param fm
	 * @return FM CMD
	 */
	public static String getFMCMD(float fm) {
		String valueOf = "";
		try {
			valueOf = String.valueOf((int) (fm * 100));
		} catch (Exception e) {
			e.printStackTrace();
			if (D)
				Log.e(TAG, "拼装FM频率指令出错！！");
		}
		return FM_HEAD + valueOf;
	}
	/**
	 * 从命令串中截取FM频道值
	 * 
	 * @param fmCMD
	 * @return
	 */
	public static float getFmVlaue(String fmCMD) { // AT#218750~10800
		if (fmCMD == null || !fmCMD.startsWith(FM_HEAD)
				&& fmCMD.length() <= FM_HEAD.length()) {
			if (D)
				Log.e(TAG, "no a FM cmd");
			return -1;
		}
		String substring = fmCMD.substring(FM_HEAD.length(), fmCMD.length());
		float floatValue = 0f;
		try {
			floatValue = Float.valueOf(substring).floatValue();
		} catch (NumberFormatException e) {
			e.printStackTrace();
			if (D)
				Log.e(TAG, "截取FM值异常！！！");
		}
		return floatValue / 100;
	}

	public static int getEqValue(String eqCMD) {
		if (!eqCMD.startsWith(EQ) || !(eqCMD.length() > EQ.length())) {
			if (D)
				Log.e(TAG, "no a EQ cmd");
			return -1;
		}
		String substring = eqCMD.substring(EQ.length(), EQ.length() + 1);
		return Integer.valueOf(substring);
	}
	/**
	 * 获取同步时间命令需要的当前时间值
	 * 
	 * @return
	 */
	public static String getCurrTimeCMD() {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
		return TIME_SYNC_HEAD + sdf.format(new Date());
	}
	/**
	 * AT#22U… UNICODE AT#22G… GBK
	 * 
	 * @return default return GBK
	 */
	public static String getMusicEncoding(String musicString) {
		String s = musicString.substring(MUSIC_NAME_HEAD.length(),
				MUSIC_NAME_HEAD.length() + 1);
		if ("U".equalsIgnoreCase(s)) {
			return "UNICODE";
		}
		return "GBK";
	}
}
